package com.qiangesoft.wechat.core.service.impl;

import com.qiangesoft.wechat.config.WxMpProperties;
import com.qiangesoft.wechat.core.service.IWxMenuService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信菜单 服务类
 *
 * @author qiangesoft
 * @date 2023-09-13 10:19:18
 */
@Slf4j
@Service
public class WxMenuServiceImpl implements IWxMenuService {

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMpProperties wxMpProperties;

//    @PostConstruct
    @Override
    public String initMenu() throws WxErrorException {
        WxMpMenuService menuService = wxMpService.getMenuService();
        WxMenu wxMenu = new WxMenu();
        List<WxMenuButton> wxMenuButtonList = new ArrayList<>();
        WxMenuButton wxMenuButton = new WxMenuButton();
        wxMenuButton.setType(WxConsts.MenuButtonType.VIEW);
        wxMenuButton.setName("我的");
        wxMenuButton.setKey("phone_bind");
        wxMenuButton.setUrl(wxMpProperties.getServer() + "wechat/user/mine");
        wxMenuButtonList.add(wxMenuButton);
        wxMenu.setButtons(wxMenuButtonList);
        String create = menuService.menuCreate(wxMenu);

        log.info("Customize Menu init success!");
        return create;
    }
}
